<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of Util
 *
 * @author Binh
 */
class Util {
    public static function prepareString($str){
        mb_internal_encoding('utf-8');
        $SPECIAL_CHARACTERS = "ÀÁÂÃÈÉÊÌÍÒÓÔÕÙÚÝàáâãèéêìíòóôõùúýĂăĐđĨĩŨũƠơƯưẠạẢảẤấẦầẨẩẪẫẬậẮắẰằẲẳẴẵẶặẸẹẺẻẼẽẾếỀềỂểỄễỆệỈỉỊịỌọỎỏỐốỒồỔổỖỗỘộỚớỜờỞởỠỡỢợỤụỦủỨứỪừỬửỮữỰự";
        $REPLACEMENTS = "AAAAEEEIIOOOOUUYaaaaeeeiioooouuyAaDdIiUuOoUuAaAaAaAaAaAaAaAaAaAaAaAaEeEeEeEeEeEeEeEeIiIiOoOoOoOoOoOoOoOoOoOoOoOoUuUuUuUuUuUuUu";
        $rs = "";
        for($i=0 ; $i < mb_strlen($str); $i++){
            $idx = strpos($SPECIAL_CHARACTERS, $str[$i]);
            if($idx !== FALSE){
                $rs = $rs.$REPLACEMENTS[$idx];
                                
            }else{
                $rs = $str[$i];
            }
            echo $rs."<br/>";
        }
        return $rs;
    }
    
    public static function getRequestParam($name){
        if (isset($_REQUEST[$name])){
            if(!empty($_REQUEST[$name])){
                return $_REQUEST[$name];
            }
        }
        return NULL;
    }
    
    public static function getSessionParam($name){
        if (isset($_SESSION[$name])){
            if(!empty($_SESSION[$name])){
                return $_SESSION[$name];
            }
        }
        return NULL;
    }
    
    public static function freeSessionParam($name){
        if (isset($_SESSION[$name])){
            unset($_SESSION[$name]);
        }
    }
    
    public static function startSession(){
        if(session_id() == ''){
            //session_start();
        }
    }
    
    public static function getShortContent($content, $searcher, $length){
        $idx1 = mb_strpos($content, '<img');
        $shortContent = '';
        if($idx1 < $length ){
            $idx2 = mb_strpos($content, '>', $idx1);
            $idx3 = mb_strpos($content, $searcher, $idx2 + 50);
            $shortContent = mb_substr($content, 0, $idx1).mb_substr($content, $idx2 + 1, $idx3);
        }else if($idx1 > $length ){
            $shortContent = mb_substr($content, 0, $idx1);
        }else{
            $shortContent = mb_substr($content, 0, $idx1-1);
        }
        $shortContent = $shortContent.'...';
        return $shortContent;
    }
}

?>
